﻿app.filter('startFrom', function () {
    return function (input, start) {
        if (input) {
            start = +start; //parse to int
            return input.slice(start);
        }
        return [];
    }
});
app.controller('ChurchController', ['$scope', 'GlobalService', 'ApplicationService', 'alertboxservice', 'fileUpload', function ($scope, GlobalService, ApplicationService, alertboxservice, fileUpload) {
    
    BindGrid();

    $scope.toggle = function (modelstate, id) {
        $scope.modelstate = modelstate;
        $scope.id = id;
        switch (modelstate) {
            case 'add':
                $scope.title = "Add Church Details";
                $scope.church = null; 
                break;
            case 'edit':
                $scope.title = "Edit Church Details";
                url = "Church/GetChruchDetail";
                ApplicationService.Getdata(url, id).success(function (data) {
                    $scope.church = data;
                });
                break;
        }
        $("#Church").modal({ backdrop: 'static' });
    }

    $scope.filter = function () {
        $timeout(function () {
            $scope.filteredItems = $scope.filtered.length;
        }, 10);
    };

    $scope.churchsummary = function () {
        url = "Church/GetChurchSummary";
        ApplicationService.Getdata(url).success(function (data) {
            $scope.summary = data;
        });
    }

    $scope.delete = function (data, dataid) {
        swal({
            title: "Are you sure?",
            text: "Your will not be able to recover " + data + " Details!",
            type: "warning",
            showCancelButton: true,
            cancelButtonClass: 'btn-outline btn-black mr-10',
            cancelButtonText: 'Cancel',
            confirmButtonClass: "btn-danger",
            confirmButtonText: "Delete",
            closeOnConfirm: false
        }, function (status) {
            if (status) {
                url = "Church/DeleteChurch";
                ApplicationService.DeleteData(url, dataid, data).success(function (data) {
                    if (data == "DSUS") {
                        swal("Deleted!", "Details deleted successfully", "success");
                    }
                    else {
                        swal("failed", "Unable to delete this details", "error");
                    }
                    BindGrid();
                });
            }
        });
        
        
    }


    function BindGrid() {
        url = "Church/GetChurches";
        ApplicationService.Getdatas(url).success(function (data) {
            $scope.list = data;
            $scope.currentPage = 1; 
            $scope.entryLimit = 10; 
            $scope.filteredItems = $scope.list.length; 
            $scope.totalItems = $scope.list.length;
        });
    }

    $scope.setPage = function (pageNo) {
        $scope.currentPage = pageNo;
    };

    $scope.sort_by = function (predicate) {
        $scope.predicate = predicate;
        $scope.reverse = !$scope.reverse;
    };

   
    $scope.save = function (modelstate, id) {
        $scope.buttonstate = true;
        var url;
        switch (modelstate) {
            case 'add':
                url = "Church/SaveChurchDetails";
                break;
            case 'edit':
                url = "Church/UpdateChurchDetails";
                break;
        }
        var uploadUrl = "/CommonDataService/DataService/Uploadfile";
        if ($scope.church.Image != null) {
            var file = $scope.church.Image;
            if (file.name != null) {
                fileUpload.uploadFileToUrl(file, uploadUrl);
                $scope.church.Image = file.name;
            }
        }
        ApplicationService.Save($scope.church, url).success(function(result){
            alertboxservice.ShowMsg(result, "Church ");
            $scope.buttonstate = false;
            BindGrid();
        });
    }
    

    $scope.$watch('church.OrganizationId', function (id) {
        GlobalService.GetChurchDesignation(id).success(function (data) {
            $scope.designation = data;
        });
        GlobalService.GetChurchZones(id).success(function (data) {
            $scope.zones = data;
        });
    });

    GlobalService.GetMinitrialType().success(function (data) {
        $scope.minitrialtype = data;
    });


    GlobalService.GetCountry().success(function (data) {
        $scope.countries = data;
    });

    $scope.$watch('church.ChurchContact.CountryId', function (newVal) {
        GlobalService.GetStates(newVal).success(function (data) {
            $scope.states = data;
        });
    });

    GlobalService.GetGrade().success(function (data) {
        $scope.grade = data;
    });

    
}]);

